﻿Imports System.Data
Imports System.Data.Odbc
Imports System.Text
Imports System.Collections.Generic
Imports WSEntidades
Imports WSFactory

Public Class AccesosDAL
    Public Function registraAcceso(ByVal pAcceso As AccesosBE) As Integer
        Dim instrSql As String
        Dim oInsParm(0 To 3) As OdbcParameter
        Try

            instrSql = "INSERT INTO ""Accesos""(""IdEmpresa"", ""IdCaja"", ""Fecha"", ""IPRemota"" " & _
                                        "VALUES (?,?,?,?);"

            oInsParm(0) = New OdbcParameter("", pAcceso.IdEmpresa)
            oInsParm(1) = New OdbcParameter("", pAcceso.IdCaja)
            oInsParm(2) = New OdbcParameter("", pAcceso.Fecha)
            oInsParm(3) = New OdbcParameter("", pAcceso.IPRemota)

            'Crea el objeto connection
            Using myConn As OdbcConnection = New OdbcConnection(My.Settings.cs)
                Using myComm As New OdbcCommand(instrSql, myConn)
                    myComm.CommandType = CommandType.Text
                    myComm.Parameters.AddRange(oInsParm)
                    'If (myConn.State = ConnectionState.Closed) Then
                    myConn.Open()
                    'End If
                    myComm.ExecuteNonQuery()
                    If (myComm.ExecuteNonQuery() > 0) Then
                        Return True
                    Else
                        Return False
                    End If
                End Using
            End Using
        Catch ex As Exception
            Throw
        Finally
            oInsParm = Nothing
            instrSql = Nothing

        End Try
    End Function

    'Public Function getAccesos(ByVal pIdEmpr As Integer, ByVal pIdCaja As Integer) As List(Of AccesosBE)
    '    Dim accesosLs As List(Of AccesosBE) = New List(Of AccesosBE)
    '    Dim instrSql As String
    '    Try
    '        instrSql = "SELECT ""IdEmpresa"", ""IdCaja"", ""Fecha"", ""IPRemota"" " & _
    '                                "FROM ""Accesos"" " & _
    '                               "WHERE ""IdEmpresa"" = ? " & _
    '                                   "AND ""IdCaja"" = ? ;"

    '        Using myConn As OdbcConnection = New OdbcConnection(My.Settings.cs)
    '            Using myComm As OdbcCommand = New OdbcCommand(instrSql, myConn)
    '                myComm.CommandType = CommandType.Text
    '                myComm.Parameters.Add(New OdbcParameter("", pIdEmpr))
    '                myComm.Parameters.Add(New OdbcParameter("", pIdCaja))
    '                myConn.Open()
    '                Using myDR As OdbcDataReader = myComm.ExecuteReader()
    '                    While myDR.Read()
    '                        Dim accesos As AccesosBE = accesosFact.Build(myDR)
    '                        If (Not accesos Is Nothing) Then
    '                            accesosLs.Add(accesos)
    '                        End If
    '                        accesos = Nothing
    '                    End While
    '                End Using
    '            End Using
    '        End Using
    '        Return accesosLs

    '    Catch ex As Exception
    '        Throw 
    '    Finally
    '        accesosLs = Nothing
    '        instrSql = Nothing
    '    End Try
    'End Function

    'Public Function getAccesosByEmpr(ByVal pIdEmpr As Integer) As List(Of AccesosBE)
    '    Dim accesosLs As List(Of AccesosBE) = New List(Of AccesosBE)
    '    Dim instrSql As String
    '    Try
    '        instrSql = "SELECT ""IdEmpresa"", ""IdCaja"", ""Fecha"", ""IPRemota"" " & _
    '                                 "FROM ""Accesos"" " & _
    '                                "WHERE ""IdEmpresa"" = ?;"

    '        Using myConn As OdbcConnection = New OdbcConnection(My.Settings.cs)
    '            Using myComm As OdbcCommand = New OdbcCommand(instrSql, myConn)
    '                myComm.CommandType = CommandType.Text
    '                myComm.Parameters.Add(New OdbcParameter("", pIdEmpr))
    '                myConn.Open()
    '                Using myDR As OdbcDataReader = myComm.ExecuteReader()
    '                    While myDR.Read()
    '                        Dim accesos As AccesosBE = accesosFact.Build(myDR)
    '                        If (Not accesos Is Nothing) Then
    '                            accesosLs.Add(accesos)
    '                        End If
    '                        accesos = Nothing
    '                    End While
    '                End Using
    '            End Using
    '        End Using
    '        Return accesosLs

    '    Catch ex As Exception
    '        Throw 
    '    Finally
    '        accesosLs = Nothing
    '        instrSql = Nothing
    '    End Try
    'End Function

End Class
